#include "../include/IOByteString.h"

#include "../include/Tool.h"

#include "../include/MsgQueue.h"

namespace ZY {

    IOByteString::~IOByteString()
    {
        //dtor

    }


    void IOByteString::send()
    {
        if ( sock ) {
//            io->send( *this );

            sock->send( *this );
        } else {
            clear();
        }
    }

    void IOByteString::nativeSend()
    {
        if ( sock ) {
            sock->nativeSend(*this);
        } else {
            clear();
        }
    }


    void ThreadIOByteString::send()
    {
        ByteString bs;

        bs.setInt32( sock->getFd() );
        bs.setInt64( (int64_t) this );

//        string msgName = "thread_";
//        Tool::stringAppend( msgName , pthread_self() );

        MsgQueue msg ;

        msg.add( mainSend , bs );

        msg.merginTo( MsgQueue::getInstance( MsgQueueType::SWAP_CACHE_QUEUE ) );
    }

    void ThreadIOByteString::nativeSend()
    {
        ByteString bs;

        bs.setInt32( sock->getFd() );
        bs.setInt64( (int64_t) this );

        MsgQueue msg;

        msg.add( mainNativeSend , bs );

        msg.merginTo( MsgQueue::getInstance( MsgQueueType::SWAP_CACHE_QUEUE ) );
    }

    void ThreadIOByteString::mainSend(ByteString & bs )
    {
        int fd;
        IOByteString *ibs;
        Sock *sock;

        fd = bs.getInt32();
        ibs = (IOByteString *) bs.getInt64();

        sock = Sock::getPointer( fd );

        if ( sock ) {
            sock->send( ibs );
        }

        delete ibs;
    }

    void ThreadIOByteString::mainNativeSend( ByteString & bs )
    {
        int fd;
        IOByteString *ibs;
        Sock *sock;

        fd = bs.getInt32();
        ibs = (IOByteString *) bs.getInt64();

        sock = Sock::getPointer( fd );

        if ( sock ) {
            sock->nativeSend( ibs );
        }

        delete ibs;
    }
}
